<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('出资人现金流') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('基金简称')" prop="fundName">
        <el-input v-model="queryParams.fundName" :placeholder="$t('基金简称')" />
      </el-form-item>
      <el-form-item :label="$t('现金流类型')" prop="type">
        <el-select v-model="queryParams.type" :placeholder="$t('现金流类型')" clearable>
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('发生时间')" prop="operateDate">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />

      </el-form-item>
      <el-form-item :label="$t('现金流方向')" prop="direction">
        <el-select v-model="queryParams.direction" :placeholder="$t('现金流方向')" clearable>
          <el-option
            v-for="dict in directionOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('业务类型')" prop="businessType">
        <el-select v-model="queryParams.businessType" :placeholder="$t('业务类型')" clearable>
          <el-option
            v-for="dict in businessTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('IRR计算')" prop="whetherIrr">
        <el-select v-model="queryParams.whetherIrr" :placeholder="$t('IRR计算')" clearable>
          <el-option
            v-for="dict in whetherIrrOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['lpbase:cashflow:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['lpbase:cashflow:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['lpbase:cashflow:view']"-->
      <!--          type="primary"-->
      <!--          icon="el-icon-info"-->
      <!--          size="mini"-->
      <!--          @click="getIrr"-->
      <!--        >{{ $t("获取IRR") }}</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="lpCashflowList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金简称')" align="center" prop="fundName" show-overflow-tooltip />
      <el-table-column :label="$t('现金流类型')" align="center" prop="type" :formatter="typeFormat" show-overflow-tooltip width="120" />
      <el-table-column :label="$t('发生时间')" align="center" prop="operateDate" show-overflow-tooltip width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operateDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('发生金额') + $t('(元)')" align="center" prop="operateAmount" show-overflow-tooltip width="120">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.operateAmount, undefined, 2, true) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('方向')" align="center" prop="direction" :formatter="directionFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('业务类型')" align="center" prop="businessType" :formatter="businessTypeFormat" show-overflow-tooltip width="120" />
      <el-table-column :label="$t('是否参与IRR计算')" align="center" prop="whetherIrr" :formatter="whetherIrrFormat" show-overflow-tooltip width="130" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot="header">
          <span>{{ $t('操作') }}</span>
          <!--          <el-tooltip placement="top">-->
          <!--            <div slot="content">-->
          <!--              {{ $t('不是从现金流功能添加的数据，不能修改与删除，如需修改请修改来源数据') }}-->
          <!--            </div>-->
          <!--            <i class="el-icon-question" />-->
          <!--          </el-tooltip>-->
        </template>
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['lpbase:cashflow:edit']"
            size="small"
            type="text"
            :icon=" 'el-icon-edit'"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['lpbase:cashflow:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            :disabled="!!scope.row.dataId"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出资人现金流对话框 -->
    <el-dialog :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <lpCashflowForm ref="lpCashflowForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listLpCashflow, delLpCashflow, getLpCashflowIrr } from '@/api/fund/lpBase/cashflow/lpCashflow'
import lpCashflowForm from './lpCashflowForm.vue'
export default {
    components: {
        lpCashflowForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 出资人现金流表格数据
          lpCashflowList: [],
          // 弹出层标题
          title: '',
          // 是否显示弹出层
          formOpen: false,
          // 发生时间范围
          dateRange: [],
          // 现金流类型  预计 实际字典
          typeOptions: [],
          // 方向：流入、流出字典
          directionOptions: [],
          // 业务类型：基金缴款 基金分配字典
          businessTypeOptions: [],
          // 是否参与IRR计算字典
          whetherIrrOptions: [],
          // 查询参数
          queryParams: {
              type: undefined,
              lpId: this.$route.query.lpId,
              operateDate: undefined,
              direction: undefined,
              businessType: undefined,
              whetherIrr: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          }
      }
  },
  created() {
      this.getList()
      this.getDicts('cash_flow_type').then(response => {
          this.typeOptions = response.data
      })
      this.getDicts('cash_flow_direction').then(response => {
          this.directionOptions = response.data
      })
      this.getDicts('lpbase_cashflow_business_type').then(response => {
          this.businessTypeOptions = response.data
      })
      this.getDicts('sys_yes_no').then(response => {
          this.whetherIrrOptions = response.data
      })
  },
  methods: {
    /** 查询出资人现金流列表 */
    getList() {
        this.loading = true
        listLpCashflow(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.lpCashflowList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 现金流类型  预计 实际字典翻译
    typeFormat(row) {
      return this.selectDictLabel(this.typeOptions, row.type)
    },
    // 方向：流入、流出字典翻译
    directionFormat(row) {
      return this.selectDictLabel(this.directionOptions, row.direction)
    },
    // 业务类型：基金缴款 基金分配字典翻译
    businessTypeFormat(row) {
      return this.selectDictLabel(this.businessTypeOptions, row.businessType)
    },
    // 是否参与IRR计算字典翻译
    whetherIrrFormat(row) {
      return this.selectDictLabel(this.whetherIrrOptions, row.whetherIrr)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            lpId: undefined,
            fundId: undefined,
            type: undefined,
            operateDate: undefined,
            operateAmount: undefined,
            direction: '0',
            businessType: undefined,
            whetherIrr: '0',
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.dateRange = []
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.title = this.$t('出资人现金流')
        this.$nextTick(() => {
            this.$refs.lpCashflowForm.init(null)
        })
    },
    getIrr() {
      getLpCashflowIrr(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        let msg = ''
        if (res.data === 502) {
          msg = this.$t('计算失败：小于或等于一条现金流')
        } else if (res.data === 503) {
          msg = this.$t('计算失败：第一条现金流的时间不是最早时间')
        } else if (res.data === 504) {
          msg = this.$t('计算失败：第一条现金流的payment的值不为负')
        } else {
          msg = res.data
        }
        this.$alert(msg, '当前数据IRR', {
          dangerouslyUseHTMLString: true
        })
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.title = this.$t('出资人现金流')
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.lpCashflowForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delLpCashflow(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/cashflow/lpCashflow/export', this.addDateRange(this.queryParams, this.operateDates))
    }
  }
}
</script>
